<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<title></title>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js"></script>
<script type="text/javascript">
	var userId = '${__SATURN_AUTORITY_USER__.id}';
	Ext.onReady(function(){
		var resultWidth = Ext.get("task-grid").getWidth();
		Ext.QuickTips.init();
		var formPanel = new Ext.FormPanel({
			frame: true,
	        title:'待处理的任务',
	        labelWidth:55,
	        renderTo:'form-ct',
	        width:resultWidth,
			items: [{
	              xtype:'fieldset',
           		  title: '选项',
          		  autoHeight: true,
          		  width:resultWidth,
            	  layout: 'form',
		     	  items: [{　
	                xtype: 'checkboxgroup',
	                fieldLabel: '任务来源',  
	                items: [
	                    {boxLabel: '我的任务',id: 'cbSelfTask', checked: true},
	                    {boxLabel: '代理任务', id: 'cbAgentTask',checked: true},
	                    {boxLabel: '代办任务', id: 'cbDelegateTask',checked: true},
	                    {boxLabel: '协办任务', id: 'cbHelpTask',checked: true}
	                ]
            	}]
            }],
             buttons: [{
	            text: '查询',
	            handler:function(){
	            	if(isValid()){
	            		var task = getTaskfrom();          		
	            		store.load({params:{	
	            						start:0,
	            						limit:20,	
										userid:userId,
										wi_state:task }}); 
	            	}else{
	            		Ext.MessageBox.alert("请选择任务来源");
	            	}
	            }
       		 }]
		});
		
		function getTaskfrom(){
			var cbSelfTask = Ext.get("cbSelfTask").dom.checked;
			var cbAgentTask = Ext.get("cbAgentTask").dom.checked;
			var cbDelegateTask = Ext.get("cbDelegateTask").dom.checked;
			var cbHelpTask = Ext.get("cbHelpTask").dom.checked;
			var task = "";
       		if(cbSelfTask){
       			task = task + "0,";
       		}
       		if(cbAgentTask){
       			task = task + "1,";
       		}
       		if(cbDelegateTask){
       			task = task + "2,";
       		}
       		if(cbHelpTask){
       			task = task + "3,";
       		}
	        return task.substring(0,task.length-1);
		}
		
		function isValid(){
			var flag = true;
			var cbSelfTask = Ext.get("cbSelfTask").dom.checked;
			var cbAgentTask = Ext.get("cbAgentTask").dom.checked;
			var cbDelegateTask = Ext.get("cbDelegateTask").dom.checked;
			var cbHelpTask = Ext.get("cbHelpTask").dom.checked;
			if(cbSelfTask==false&&cbAgentTask==false&&cbDelegateTask==false&&cbHelpTask==false){
			   flag = false;
			}
			return flag;
		}
		
		var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/QueryUserTask.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
	               {name: 'workitem_name', mapping: 'workitem_name'},
	               {name: 'process_instance_id', mapping: 'process_instance_id'},
		    	   {name: 'workitem_id', mapping: 'workitem_id'},
	               {name: 'process_define_name', mapping: 'process_name'},
	               {name: 'process_instance_name', mapping: 'process_instance_name'},
	               {name: 'workitem_state', mapping: 'workitem_state'},
	               {name: 'workitem_start_time', mapping: 'workitem_start_time'},
	               {name: 'wi_state', mapping: 'wi_state'},
	               {name: 'bizstate', mapping: 'bizstate'},
	               {name: 'workitem_is_timeout', mapping: 'workitem_is_timeout'},
	               {name: 'act_inst_limit_number', mapping: 'act_inst_limit_number'}
	           ]
		});
	  
	    var grid = new Ext.grid.GridPanel({
       	    store: store,
	        columns: [
	       	    {header: "操作", width:resultWidth*0.1, dataIndex: 'workitem_id', sortable: false,renderer:showOp},
	            {header: "工作项名称", width: resultWidth*0.1, dataIndex: 'workitem_name', sortable: true,renderer:showDetail},
	            {header: "业务流程名称", width: resultWidth*0.1, dataIndex: 'process_define_name', sortable: true},
	            {header: "流程实例名称", width: resultWidth*0.1, dataIndex: 'process_instance_name', sortable: true},
	            {header: "当前状态", width: resultWidth*0.1, dataIndex: 'workitem_state', sortable: true,renderer:showState},
	            {header: "启动时间", width: resultWidth*0.15, dataIndex: 'workitem_start_time', sortable: true},
	            {header: "是否超时", width: resultWidth*0.1, dataIndex: 'workitem_is_timeout', sortable: true,renderer:showIsTimeout},
	            {header: "时间限制", width: resultWidth*0.1, dataIndex: 'act_inst_limit_number', sortable: true},
	            {header: "设置", width:resultWidth*0.1, dataIndex: 'wi_state', sortable: false,renderer:isDelegate}
	        ],
	        renderTo:'task-grid',
	        height:400,
	        width:resultWidth,
	        collapsible:true,
	        loadMask: {msg:'正在加载数据，请稍侯……'}, 
	        frame:true,
	        bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
		    }),
		    tbar:new Array(
			   new Ext.Toolbar.Button({
						   text:'只显示超时任务',
						   xtype:"button" ,
						   tooltip:"只显示超时任务" ,
						   handler: queryTaskOfisTimeout,
						   scope :this})
			   
		   ) 
    });
   
   	store.load({params: {start:0,limit:20,wi_state:'0,1,2,3',userid:userId}});
 	
    store.on('beforeload',function(){
    	if(isValid()){
    		var task = getTaskfrom();
		 	Ext.apply(this.baseParams,{userid:userId,wi_state:task});
         }else{
	         Ext.MessageBox.alert("请选择任务来源");
	         return false;
         }   
     });
    	
	function queryTaskOfisTimeout(){
		store.load({params: {workitem_is_timeout:'Y'}});
	}
	
   	function showOp(value,cellmeta,record){	
			return "<a href='javascript:doView("+record.data["process_instance_id"]+")'>"+"查看"+"  </a><a href='javascript:doTask("+value+")'>"+"执行"+"</a>";	
	}	
	
	function showDetail(value,cellmeta,record){
		return "<a href='javascript:showWokitemInfo2("+record.data["workitem_id"]+")'>"+value+"</a>";
	}
	
	function showIsTimeout(value){
		if(value=="Y"){
			return "<font color='red'>"+"是"+"</font>"
		}else{
			return "否";
		}
	}
	function isDelegate(value,cellmeta,record){
		var bizstate = record.data["bizstate"];
		var wi_state = record.data["wi_state"];
	 	//if(bizstate=='1'||bizstate=='0'||bizstate=='5'||bizstate=='7'){
	 	//if(bizstate=='0'||bizstate=='5'||bizstate=='7'){
	 	if((bizstate=='0'||bizstate=='5'||bizstate=='7')&&wi_state!='1'){
	 		return "<a href='javascript:setHelp("+record.data["workitem_id"]+")'>协办</a>&nbsp;&nbsp;<a href='javascript:setDelegate("+record.data["workitem_id"]+")'>"+"代办</a>";	
	 	}else if(bizstate=='4'){
	 		return "<a href='javascript:setRedo("+record.data["workitem_id"]+")'>重做</a>";	
	 	}else if(bizstate=='2'||bizstate=='3'||bizstate=='6'){
	 		return "<a href='javascript:setReject("+record.data["workitem_id"]+")'>拒绝</a>";	
	 	}
	}	
	
	function showState(value){
		if(value=='10')
			return "运行";
		if(value=='12')
			return "完成";
	}
});

	function doTask(workitem_id){
		var form = document.forms[0];
		 form.action="${pageContext.request.contextPath}/edu.ccut.saturn.workflow::/publicModule/action/ExecuteActivity.action?workitem_id="+workitem_id;
		 form.submit();
	}
	function doView(process_instance_id){
		 var form = document.forms[0];
		 form.action="${pageContext.request.contextPath}/edu.ccut.saturn.workflow::/publicModule/action/DelineateWorkFlow.action?process_instance_id="+process_instance_id;
		 form.submit();
	}
	


	function showWokitemInfo2(workitem_id){
		window.location="${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client/myTaskModule/page/workitemOpMesg.jsp?workitem_id="+workitem_id;
	}

	function setHelp(workitem_id){
		 var panel = new Ext.FormPanel({
		 	frame:true,
		 	layout:'form',
		    items: [{
	            layout:'column',
	            items: [{
	            	columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '协办人',
		                    name: 'delegate_name',
		                    id: 'delegate_name',
		                    allowBlank:false,
		                    msgTarget:'qtip',
		                    readOnly:true
		                },{
		                    xtype:'hidden',
		                    name: 'workitem_id',
		                    id: 'workitem_id',
		                    value:workitem_id
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_to',
		                    id: 'delegate_to'
		                },{
		                    xtype:'textarea',
		                    fieldLabel: '协办原因',
		                     vtype:'isNumberAlphaUnderlineChinese',
		                    width:130,
		                    allowBlank:false,
		                    msgTarget:'qtip',
		                    maxLength:20,
		                    name: 'delegate_reason',
		                    id: 'delegate_reason'
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_to_user_type',
		                    id: 'delegate_to_user_type'
		                }]
		            },{
	                columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'button',
		                    text: '选择协办人',
		                     handler:choosePerson
		                }]
	            }]
        }],
             buttons: [{
	            text: '确认',
	            handler:function(){
	             if(panel.form.isValid()){
	            	Ext.Ajax.request({
					     method : 'POST',                           
					     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/SetDelegate.action',     
					     success:function(request){     
						       window.close(); 
						       var myResult = Ext.util.JSON.decode(request.responseText);
						       Ext.Msg.alert('信息',myResult.message, function(){
						      		 document.location.reload();
						       });
					     },
					     failure:function(){                          
					      	   Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
					     },
					     params:{
					     		delegate_to:Ext.get("delegate_to").dom.value, 
					     		delegate_from:userId, 
					     		delegate_to_user_type:Ext.get("delegate_to_user_type").dom.value,
					     		workitem_id:Ext.get("workitem_id").dom.value,
					     		delegate_reason:Ext.get("delegate_reason").dom.value,
					     		operate_type:3
					     } 
	     			});
	     			}
	            }
       		 },{
	            text: '重置',
	            handler:function(){
	            	panel.getForm().reset();
	            }
	        }]
         });
        
         var window = new Ext.Window({
            title: '设置代办',
			width: 500,
			height:200,
			layout: 'fit',
			plain:true,
			items:panel,
			buttonAlign:'center',
			modal:true
           });
           
		window.show();
	}
	
	
	function setDelegate(workitem_id){
		 var panel = new Ext.FormPanel({
		 	frame:true,
		 	layout:'form',
		    items: [{
	            layout:'column',
	            items: [{
	            	columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '代办人',
		                    name: 'delegate_name',
		                    id: 'delegate_name',
		                   allowBlank:false,
		                    msgTarget:'qtip',
		                    readOnly:true
		                },{
		                    xtype:'hidden',
		                    name: 'workitem_id',
		                    id: 'workitem_id',
		                    value:workitem_id
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_to',
		                    id: 'delegate_to'
		                },{
		                    xtype:'textarea',
		                    fieldLabel: '代办原因',
		                     vtype:'isNumberAlphaUnderlineChinese',
		                    width:130,
		                    allowBlank:false,
		                    msgTarget:'qtip',
		                    maxLength:20,
		                    name: 'delegate_reason',
		                    id: 'delegate_reason'
		                },{
		                    xtype:'hidden',
		                    name: 'delegate_to_user_type',
		                    id: 'delegate_to_user_type'
		                }]
		            },{
	                columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'button',
		                    text: '选择代办人',
		                     handler:choosePerson
		                }]
	            }]
        }],
             buttons: [{
	            text: '确认',
	            handler:function(){
	            if(panel.form.isValid()){
	            	Ext.Ajax.request({
					     method : 'POST',                           
					     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/SetDelegate.action',     
					     success:function(request){     
						       window.close(); 
						       var myResult = Ext.util.JSON.decode(request.responseText);
						       Ext.Msg.alert('信息',myResult.message, function(){
						      		 document.location.reload();
						       });
					     },
					     failure:function(){                          
					      	   Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
					     },
					     params:{
					     		delegate_to:Ext.get("delegate_to").dom.value, 
					     		delegate_from:userId, 
					     		delegate_to_user_type:Ext.get("delegate_to_user_type").dom.value,
					     		workitem_id:Ext.get("workitem_id").dom.value,
					     		delegate_reason:Ext.get("delegate_reason").dom.value,
					     		operate_type:2
					     } 
	     			});
	     		   }
	            }
       		 },{
	            text: '重置',
	            handler:function(){
	            	panel.getForm().reset();
	            }
	        }]
         });
        
         var window = new Ext.Window({
            title: '设置代办',
			width: 500,
			height:200,
			layout: 'fit',
			plain:true,
			items:panel,
			buttonAlign:'center',
			modal:true
           });
           
		window.show();
	}
	

	 function choosePerson(){
			var root=new Ext.tree.AsyncTreeNode({
				id:"root_participant",
				text:"参与者"});
			var tree=new Ext.tree.TreePanel({
				root:root,
				loader: new Ext.tree.TreeLoader({url:'${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/agentModule/action/GetTreeJson.action'}),
				width:400,
				autoScroll: true
			});	

		  tree.on('click', function(node){
					addDelegate(node.id,node.text);
				},this);
	       var window = new Ext.Window({
				title: '添加代办人',
				width: 400,
				height:300,
				minWidth: 300,
				minHeight: 200,
				layout: 'fit',
				plain:true,
				items:tree,
				buttonAlign:'center',
				modal:true
			});
			window.show();
		}
		
		
	function addDelegate(userid,name){
		if(userid=='root_participant'){
			return false;
		}
		 if(userid!='rootDept'&& userid!='rootRole'){
		 	var type = userid.split('_')[0];
		 	var id = userid.split('_')[1];
		 	if((type=='deptUser'||type=='roleUser')&&id!=userId){
		 		Ext.get("delegate_to_user_type").dom.value ="user";
		 		Ext.get("delegate_to").dom.value =id;
		 	    Ext.get("delegate_name").dom.value =name;
		 	}
		 	if(type=='dept'){
		 		Ext.get("delegate_to_user_type").dom.value ="dept";	
		 		Ext.get("delegate_to").dom.value =id;
		 	    Ext.get("delegate_name").dom.value =name; 		
		 	}
		 	if(type=='role'){
		 		Ext.get("delegate_to_user_type").dom.value ="role";
		 		Ext.get("delegate_to").dom.value =id;
		 	    Ext.get("delegate_name").dom.value =name;
		 	}
		 }
	 }
	 
	function setRedo(workitem_id){
		 var panel = new Ext.FormPanel({
		 	frame:true,
		 	layout:'form',
		 	labelWidth:60,
		    items: [{          
                  xtype:'textarea',
                  fieldLabel: '重做原因',
                  vtype:'isNumberAlphaUnderlineChinese',
                  width:330,
                  maxLength:20,
                  allowBlank:false,
                  msgTarget:'qtip',
                  maxLength:20,
                  name: 'reason',
                  id: 'reason'
       		 }],
             buttons: [{
	            text: '确认',
	            handler:function(){
	             if(panel.form.isValid()){
	            	Ext.Ajax.request({
					     method : 'POST',                           
					     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/SetRedo.action',     
					     success:function(request){     
						       window.close(); 
						       var myResult = Ext.util.JSON.decode(request.responseText);
						       Ext.Msg.alert('信息',myResult.message, function(){
						      		 document.location.reload();
						       });
					     },
					     failure:function(){                          
					      	   Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
					     },
					     params:{
					     		userId:userId,
					     		workitem_id:workitem_id,
					     		reason:Ext.get("reason").dom.value
					     } 
	     			});
	     			}
	            }
       		 },{
	            text: '重置',
	            handler:function(){
	            	panel.getForm().reset();
	            }
	        }]
         });
        
         var window = new Ext.Window({
            title: '重做',
			width: 440,
			height:140,
			layout: 'fit',
			plain:true,
			items:panel,
			buttonAlign:'center',
			modal:true
           });
           
		window.show();
	}
	
	function setReject(workitem_id){
		 var panel = new Ext.FormPanel({
		 	frame:true,
		 	layout:'form',
		 	labelWidth:60,
		    items: [{
                    xtype:'textarea',
                    fieldLabel: '拒绝原因',
                    allowBlank:false,
                    vtype:'isNumberAlphaUnderlineChinese',
                    msgTarget:'qtip',
                    width:300,
                    maxLength:20,
                    name: 'reason',
                    id: 'reason'
       		 }],
             buttons: [{
	            text: '确认',
	            handler:function(){
	             if(panel.form.isValid()){
	            	Ext.Ajax.request({
					     method : 'POST',                           
					     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/SetReject.action',     
					     success:function(request){     
						       window.close(); 
						       var myResult = Ext.util.JSON.decode(request.responseText);
						       Ext.Msg.alert('信息',myResult.message, function(){
						      		 document.location.reload();
						       });
					     },
					     failure:function(){                          
					      	   Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
					     },
					     params:{
					     		userId:userId,
					     		workitem_id:workitem_id,
					     		reason:Ext.get("reason").dom.value
					     } 
	     			});
	     			}
	            }
       		 },{
	            text: '重置',
	            handler:function(){
	            	panel.getForm().reset();
	            }
	        }]
         });
        
         var window = new Ext.Window({
            title: '拒绝',
			width: 440,
			height:140,
			layout: 'fit',
			plain:true,
			items:panel,
			buttonAlign:'center',
			modal:true
           });
           
		window.show();
	}
</script>
<div id='form-ct' width='100%'></div>
<div id='task-grid' width='100%'></div>
</head>
</html>